﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 星球
public class Star : MonoBehaviour {

    public GameObject blueBoxPrefab;//宝箱预设体
    public GameObject redBoxPrefab;//宝箱预设体
    public GameObject greenBoxPrefab;//宝箱预设体
    public GameObject pinkBoxPrefab;//宝箱预设体
    public GameObject purpleBoxPrefab;//宝箱预设体

    private int insBoxNum = 3;//星球被突破后爆出的宝箱数量

    private float currHp;//当前血量
	private float recoverHP;
	public Slider hpSlider;

	private float defendPower = 50;//防御力
	private float hp = 600;//血
	private float defendHP = 200;//防御罩的血
	
	
	private float searchRange = 80;//搜索范围
	private Sprite prefab_body;//战斗界面贴图
	private string prefab_explosion;//爆炸贴图
	private Color color;//颜色
	public int state = state_defend;//状态

	public static int state_defend = 0;//警戒状态
	public static int state_attack = 1;//攻击状态
	public static int state_death = 2;//死亡状态

	private int starPower = 1;//星球技术力（卫星强度，护盾血量，星球护甲， 星球血量，搜索范围，存储的敌机数量，敌机产生时间，是否有boss）
	private int producePower = 1;//造船技术力（速度，血量，防御）
	private int planePower = 1;//兵装技术力（产生的敌机强度）

	public bool isProtege = false;

	// Use this for initialization
	void Start () {
		currHp = hp;
	}
	
	// Update is called once per frame
	void Update () {
		if(transform.Find("Shield(Clone)") != null)
	   {
		   isProtege = true;
	   }
	   else
	   {
		   isProtege = false;
	   }
	}


	//设置星球能力 
	public void SetPower(int starPower, int producePower, int planePower){
		this.starPower = starPower;
		this.producePower = producePower;
		this.planePower = planePower;
		SetStarPro();
	}


	// 星球能力变化的时候，属性随之变化
	private void SetStarPro(){

	}



	public void SetPrefab_body(Sprite prefab_body){
		this.prefab_body = prefab_body;
		gameObject.GetComponent<SpriteRenderer>().sprite = this.prefab_body;
	}

	public void SetColor(Color color){
		this.color = color;
		gameObject.GetComponent<SpriteRenderer>().color = this.color;
	}

	public void TakeDamage(float damage){
		if(!isProtege)
		{
			if(currHp <= 0)return;
			currHp -= damage;
//			print("currHp--->" + currHp);
			hpSlider.value = currHp / hp;
			if(currHp <= 0)
			{
				print("oh, i am death");
                // Destroy(gameObject);
                InstanceBox();
                state = Star.state_death;
				gameObject.tag = "Untagged";
				gameObject.GetComponent<SpriteRenderer>().color = Color.white;
			}
		}
		
	}
    //实例化宝箱
    void InstanceBox()
    {
        for(int i = 0; i < insBoxNum; i++)
        {
            Vector2 rand = Random.insideUnitCircle;
            //随机生成宝箱
            int randBoxId = Random.Range(1,6);
            GameObject boxGo=null;
            switch (randBoxId)
            {
                case 1:
                    //实例化宝箱
                    boxGo = GameObject.Instantiate(blueBoxPrefab, transform);
                    //对应的宝箱数量加1 , 用于在仓库宝箱界面 获得宝箱的类型数量,每次掠夺数据初始化为0
                    TreasureBoxManager.blueBoxNum += 1;
                    break;
                case 2:
                    boxGo = GameObject.Instantiate(greenBoxPrefab, transform);
                    TreasureBoxManager.greedBoxNum += 1;
                    break;
                case 3:
                    boxGo = GameObject.Instantiate(pinkBoxPrefab, transform);
                    TreasureBoxManager.pinkBoxNum += 1;
                    break;
                case 4:
                    boxGo = GameObject.Instantiate(purpleBoxPrefab, transform);
                    TreasureBoxManager.purpleBoxNum += 1;
                    break;
                case 5:
                    boxGo = GameObject.Instantiate(redBoxPrefab, transform);
                    TreasureBoxManager.redBoxNum += 1;
                    break;
            }
            boxGo.transform.position = transform.position + new Vector3(rand.x * 10, rand.y * 10, 0);
            //已经爆出的宝箱数量加1
            TreasureBoxManager._instance.TempBoxNumAdd();
        }
    }

	public void SetState(int starState)
	{
		this.state = starState;
	}

    //设置爆出的宝箱数量
    public void SetInstanceBoxNum(int num)
    {
        insBoxNum = num;
    }






	public int GetStarPower(){
		return starPower;
	}
	public int GetProducePower(){
		return producePower;
	}
	public int GetPlanePower(){
		return planePower;
	}

	public float GetHp()
	{
		return hp;
	}

}
